package com.zhyonk.controller;

import com.zhyonk.config.propreties.HtmlToPdf;
import com.zhyonk.domain.AjaxResult;
import com.zhyonk.domain.School;
import com.zhyonk.service.GenNoticeService;
import com.zhyonk.service.SchoolService;
import com.zhyonk.util.ImageBase64Utils;
import com.zhyonk.util.OsInfo;
import com.zhyonk.util.Pdf2PngUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author cbl
 * @description com.zhyonk.controller
 * @date 2020/7/10
 */
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private com.zhyonk.service.impl.HtmlToPdfService HtmlToPdfService;

    @Autowired
    private GenNoticeService genNoticeService;

    @Autowired
    private HtmlToPdf HtmlToPdf;

    @Autowired
    private SchoolService schoolService;

    /**
     * 获取通知书图片信息
     * @param param
     * @return
     */
    @RequestMapping(value = "/genNoticeByName",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public AjaxResult getCheckImgViolation(@RequestBody HashMap<String,Object> param){
        boolean isHightQuality = false;
        Object nameObj = param.get("name");
        if (Objects.isNull(nameObj)){
            return AjaxResult.error("名称不能为空！");
        }
        //图片质量，默认是低质量，高质量的是预留接口给下载图片的
        Object quality = param.get("heightQuality");
        if (Objects.nonNull(quality)){
            isHightQuality = true;
        }
        String name = nameObj.toString();
        //随机一个学校
        School school = schoolService.randomSchool();

        ArrayList<String> list = new ArrayList<>();
        list.add(ImageBase64Utils.NetImageToBase64(school.getImg()));
        list.add(school.getName());
        list.add(name);
        list.add(school.getName());
        String htmlInputPath = OsInfo.isWindows() ?  HtmlToPdf.getWindowsTemplatePath() : HtmlToPdf.getLinuxTemplatePath();
        try {
            String htmlOutPath = genNoticeService.fillDataToHtml(school.getScId() + "-" +name, htmlInputPath, list);
            try {

                Map<String, Object> option = new HashMap<>();
                option.put("margin-bottom","0");
                option.put("margin-left","0");
                option.put("margin-right","0");
                option.put("margin-top","0");
                option.put("page-height","125");
                option.put("page-width","88");
                String originPdfPath = HtmlToPdfService.execute(htmlOutPath, option);
                String pdf2png = Pdf2PngUtils.pdf2pngToPath(originPdfPath,
                        OsInfo.isWindows() ? HtmlToPdf.getWindowsOutputImagePath() : HtmlToPdf.getLinuxOutputImagePath(),isHightQuality);
                HashMap<String, String> map = new HashMap<>();
                map.put("imgLink", pdf2png);
                map.put("schoolName",school.getName());
                return AjaxResult.success("成功",map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("生成失败，联系管理员！");
        }
        return AjaxResult.error("生成失败，联系管理员！");
    }

}
